package gogogo.liwushuo.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.activity.HomeTitleActivity;
import gogogo.liwushuo.bean.HomeImageBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.OKHttpUtils;

public class HomeImageActivity extends AppCompatActivity {

    @InjectView(R.id.iv_image)
    ImageView ivImage;
    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.tv_toolbar)
    TextView tvToolbar;
    @InjectView(R.id.cospslayout)
    CollapsingToolbarLayout cospslayout;
    @InjectView(R.id.webview)
    WebView webview;
    @InjectView(R.id.tv_more)
    TextView tvMore;

    private int id;
    private OKHttpUtils okHttpUtils;
    private Context mContext;
    private HomeImageBean imageBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_image);
        ButterKnife.inject(this);

        mContext = this;
        okHttpUtils = OKHttpUtils.newInstance(this);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        getJsonData(id);
        setListener();
    }

    private void setListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, HomeTitleActivity.class);
                intent.putExtra("id", imageBean.getData().getColumn().getId());
                intent.putExtra("title", imageBean.getData().getColumn().getTitle());
                startActivity(intent);
            }
        });
    }

    private void getJsonData(int id) {
        okHttpUtils.getAsyncMainData(Resource.getImageIntentUrl(id), new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                imageBean = new Gson().fromJson(result, HomeImageBean.class);
                Picasso.with(mContext)
                        .load(imageBean.getData().getCover_image_url())
                        .into(ivImage);
                WebSettings settings = webview.getSettings();
                settings.setJavaScriptEnabled(true);
                settings.setAllowContentAccess(true);
                settings.setAppCacheEnabled(true);
                settings.setBlockNetworkImage(true);
                settings.setDefaultTextEncodingName("utf-8");
//                webview.setWebViewClient(new WebViewClient());
                String content = imageBean.getData().getContent_html();
//                webview.loadData(content,"text/html","UTF-8");//会乱码
                webview.setWebViewClient(new WebViewClient(){
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;
                    }
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                    }
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        super.onPageStarted(view, url, favicon);
                    }
                });
                webview.loadUrl(imageBean.getData().getUrl());

//                webview.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
            }
        });
    }
}
